<template>
    <div>
        <el-form :rules="rules" ref="loginForm" :model="loginForm" class="loginContainer">
            <h3>系统登录</h3>
            <el-form-item prop="username">
                <el-input type="text" v-model="loginForm.username" auto-complete="off" placeholder="请输入用户名："/>
            </el-form-item>
            <el-form-item prop="password">
                <el-input type="password" v-model="loginForm.password"
                          @keydown.enter.native="submitLogin" auto-complete="off" placeholder="请输入密码："/>
            </el-form-item>
            <el-checkbox v-model="checked" class="loginRemember" label="记住密码"/>
            <el-button type="primary" style="width: 100%" @click="submitLogin">登录</el-button>
        </el-form>
    </div>
</template>

<script>
    import {postKeyValueRequest} from "../utils/api";

    export default {
        name: "Login",
        data() {
            return {
                checked: true,
                loginForm: {
                    username: 'admin',
                    password: '123'
                },
                rules: {
                    username: [
                        {required: true, message: '请输入用户名', trigger: 'blur'}
                    ],
                    password: [
                        {required: true, message: '请输入密码', trigger: 'blur'}
                    ]
                }
            }
        },
        methods: {
            submitLogin() {
                //调用表单ref="loginForm"里面的验证规则，详情参考官网！
                this.$refs.loginForm.validate((valid) => {
                    if (valid) {
                        // alert('submit!');
                        postKeyValueRequest('/doLogin', this.loginForm).then(resp => {
                            if (resp) {
                                //alert(JSON.stringify(resp));
                                window.sessionStorage.setItem('user', JSON.stringify(resp.obj));
                                // $route 表示(当前路由信息对象)
                                let path = this.$route.query.redirect;
                                this.$router.replace((path == '/' || path == undefined) ? '/home' : path);
                            }
                        })
                    } else {
                        this.$message.error("请填写完整哦！");
                        return false;
                    }
                });
            }
        }
    }
</script>

<style scoped>
    .loginContainer {
        border-radius: 15px;
        background-clip: padding-box;
        margin: 180px auto;
        width: 350px;
        padding: 35px 35px 15px 35px;
        background: #ffffff;
        border: 1px solid #eaeaea;
        box-shadow: 0 0 25px #2c3e50;
    }

    .loginRemember {
        text-align: left;
        margin: 0px 0px 15px 0px;
    }
</style>